package com.puboot.module.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.puboot.module.model.LkCategory;
import com.puboot.module.mapper.LkCategoryMapper;
import com.puboot.module.service.LkCategoryService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import lombok.AllArgsConstructor;

import java.util.List;


/**
 * (LkCategory)表服务实现类
 *
 * @author Leonidax
 * @since 2024-10-11 16:05:27
 */
@Service
@AllArgsConstructor
public class LkCategoryServiceImpl extends ServiceImpl<LkCategoryMapper,LkCategory> implements LkCategoryService {
    
    private final LkCategoryMapper lkCategoryMapper;

    @Override
    @Cacheable(value = "category", key = "'comic'")
    public List<LkCategory> selectCategories() {
        return this.list();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LkCategory queryById(Integer id) {
        return lkCategoryMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param lkCategory 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<LkCategory> queryByPage(LkCategory lkCategory, PageRequest pageRequest) {
        long total = lkCategoryMapper.count(lkCategory);
        return new PageImpl<>(lkCategoryMapper.queryAllByLimit(lkCategory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param lkCategory 实例对象
     * @return 实例对象
     */
    @Override
    public LkCategory insert(LkCategory lkCategory) {
        this.lkCategoryMapper.insert(lkCategory);
        return lkCategory;
    }

    /**
     * 修改数据
     *
     * @param lkCategory 实例对象
     * @return 实例对象
     */
    @Override
    public LkCategory update(LkCategory lkCategory) {
        this.lkCategoryMapper.update(lkCategory);
        return this.queryById(lkCategory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.lkCategoryMapper.deleteById(id) > 0;
    }
}
